import Service from '@/api/httpAxios'
import exp from 'constants'
import { number } from 'echarts'

export interface ProcessParamType {
  project_id: string
  offset?: number
  limit?: number
  fleet_id?: string
  url?: string
  order_by?: string
  top?: string
  state?: string
}

export interface MonitorFleet {
  project_id: string
  fleet_id: string
}

export interface MonitorInstance {
  project_id: string
  fleet_id: string
  instance_id?: string
  health_state?: string
  duration_start?: number
  duration_end?: number
}

export interface MonitorAppProcess {
  project_id: string
  fleet_id: string
  instance_id?: string
  process_id?: string
  ip_address?: string
  duration_start?: number
  duration_end?: number
  state?: string
}

export interface MonitorServerSession {
  project_id: string
  fleet_id: string
  instance_id?: string
  process_id?: string
  ip_address?: string
  duration_start?: number
  duration_end?: number
  state?: string
  start_time?: string
  end_time?: string
  server_session_id?: string
}

class Monitor {
  getHomePage = (obj: ProcessParamType) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    if (pid) {
      const url = `/v1/${pid}/homepage/fleet-runtime?order_by=${obj.order_by ? obj.order_by : ''}`
      return Service.get<any>(url)
    }
  }
  getFleetList = (obj: ProcessParamType, obj2: any) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))

    const url = `/v1/${pid}/monitor-fleets`
    let params: { [key: string]: string | number } = {}
    for (const key in obj2) {
      if (obj2[key]) {
        params[key] = obj2[key]
      }
    }
    if (obj.offset) params['offset'] = obj.offset - 1
    if (obj.limit) params['limit'] = obj.limit
    return Service.get<any>(url, {
      params
    })

  }

  getFleetListbyCondition = (obj: ProcessParamType, obj2: any) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/monitor-fleets`
    let params: { [key: string]: string | number } = {}
    for (const key in obj2) {
      if (obj2[key]) {
        params[key] = obj2[key]
      }
    }
    if (obj.offset) params['offset'] = obj.offset - 1
    if (obj.limit) params['limit'] = obj.limit
    return Service.get<any>(url, {
      params
    })
  }
  deleteFleet = (obj: ProcessParamType) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/fleets/${obj.fleet_id}`
    return Service.delete<any>(url)
  }
  getMonitorList = (obj: ProcessParamType, obj2: any) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/fleets/${obj.fleet_id}/${obj.url}`
    let params: { [key: string]: string | number } = {}
    for (const key in obj2) {
      if (obj2[key]) {
        params[key] = obj2[key]
      }
    }
    if (obj.offset) params['offset'] = obj.offset - 1
    if (obj.limit) params['limit'] = obj.limit
    return Service.get<any>(url, {
      params
    })
  }
  getMonitorFleet = (obj: MonitorFleet) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/monitor-fleets/${obj.fleet_id}`
    return Service.get<any>(url)
  }

  getMonitorInstances = (obj: MonitorInstance) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/fleets/${obj.fleet_id}/monitor-instances`
    return Service.get<any>(url, {
      params: {
        instance_id: obj.instance_id,
        health_state: obj.health_state,
        duration_start: obj.duration_start,
        duration_end: obj.duration_end
      }
    })
  }

  getMonitorAppProcesses = (obj: MonitorAppProcess) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/fleets/${obj.fleet_id}/process-counts`
    return Service.get<any>(url)
  }

  getMonitorServerSessions = (obj: MonitorServerSession) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/fleets/${obj.fleet_id}/monitor-server-sessions`
    return Service.get<any>(url, {
      params: {
        instance_id: obj.instance_id,
        process_id: obj.process_id,
        ip_address: obj.ip_address,
        duration_start: obj.duration_start,
        duration_end: obj.duration_end,
        state: obj.state,
        start_time: obj.start_time,
        end_time: obj.end_time,
        server_session_id: obj.server_session_id
      }
    })
  }
}
export default new Monitor()
